/**
 * Created by Dalyn on 2017/2/28.
 */
import React from "react";
import './css/UserManage.css'
import InputBox from "../../common/InputBox"
import Confirm from "../../common/Confirm"
import Table from './Table'
import Createuser from './Createuser'
import Relay from "react-relay"
import CreateUserMutation from './mutation/CreateUserMutation';

/*导出组件*/
export default class UserManage extends React.Component{
    constructor(props){
        super(props);
        this.state = {
            userArr:[]
        }
    }
    componentWillMount(){
        this.Style={
            p_title:{
                width:"100%",
                height:57,
                borderBottom:"1px #ededed solid"
            },
            inputsearch:{
                backgroundColor:'red',
                width:200,
                borderWidth:'0px',
                height:30
            },
            new_Company:{
                width:130,
                height:30,
                fontSize:'14px'
            }
        }
    }
    componentDidMount(){
            this.userArr=[{
                    userId:1,
                    userName:'刘大声',
                    userCompany:['山东省德州市才一方','山东省德州市才一方','山东省德州市才一方','山东省德州市才一方'],
                    userPhone:'12345678912',
                    userState:'已开通'
                },{
                    userId:1,
                    userName:'刘大声',
                    userCompany:['山东省德州市才一方','山东省德州市才一方','山东省德州市才一方'],
                    userPhone:'12345678912',
                    userState:'已开通'
                },{
                    userId:1,
                    userName:'刘大声',
                    userCompany:['山东省德州市才一方','山东省德州市才一方','山东省德州市才一方'],
                    userPhone:'12345678912',
                    userState:'已开通'
                },{
                    userId:1,
                    userName:'刘大声',
                    userCompany:['山东省德州市才一方','山东省德州市才一方','山东省德州市才一方'],
                    userPhone:'12345678912',
                    userState:'已开通'
                }]

                this.refs.usertable.setState({
                    userContext:this.userArr
                })
    }

    //新建用户弹窗的出现
    createUser(){
        this.refs.createUser.setState({
            visible:true
        })
    }
    onCreateUser(phone,name,password){
       //创建用户调用后台方法
        Relay.Store.commitUpdate(
            new CreateUserMutation({
                //放传给后台参数的地方
                phone:phone.toString(),
                name:name,
                password:password.toString()
            }),
            {
                onFailure:()=>{
                    console.log(11110000)
                },
                onSuccess:(response)=>{
                    console.log(response.createUser.createUser);
                    if(response.createUser.createUser.errorCode==5){
                        alert('手机号已存在')
                    }
                    console.log(22222222222)
                }
               });
            this.refs.createUser.setState({
                visible:false
            })
    }


    render(){
        return (
            <div className="userManage_right">
                <div className="comManage_title">
                    {/*搜索框*/}
                    <div className="coManage_search">
                        <InputBox style={this.Style.inputsearch}></InputBox>
                        <span className="inputsearch_icon"></span>
                    </div>
                </div>
                <div className="div_context">
                    <div className="userTable_title">
                        <div className="new_User_father">
                            <Confirm tcontext={'+新建用户'} style={this.Style.new_Company} touch={this.createUser.bind(this)}></Confirm>
                        </div>
                    </div>
                    <Table ref={'usertable'}/>
                </div>
                <Createuser ref={'createUser'} createUser={this.onCreateUser.bind(this)}></Createuser>
            </div>
        )
    }
}

